from  selenium import  webdriver
from  selenium.webdriver.chrome.options import Options
from  selenium.webdriver.chrome.service import Service
from  selenium.webdriver.common.by import By
import  time as Time

option = Options()
option.add_argument("--headless")
option.add_argument("--disable-gpu")
service = Service(r"F:\桌面\win_r\浏览器驱动\chrome/chromedriver.exe")
driver = webdriver.Chrome(options=option, service=service)
url ="https://space.bilibili.com/1411243914/video?tid=0&page=1&keyword=&order=pubdate"
file =open("F:/桌面/mark2.txt","a",encoding="utf-8")
list =[]
page =1
driver.get(url)
Time.sleep(1)
while True:
    lis = driver.find_element(By.CLASS_NAME, "cube-list").find_elements(By.TAG_NAME, "li")
    for i in lis:
        href = i.find_element(By.TAG_NAME, "a").get_attribute("href")
        title = i.find_element(By.TAG_NAME, "a").find_element(By.TAG_NAME, "img").get_attribute("alt")
        time = i.find_element(By.CLASS_NAME, "time").text
        if title.__contains__("无职转生"):
            print(href, title, time)
            list.append(href+" "+title+" "+time)
    if not driver.page_source.__contains__("be-pager-next be-pager-disabled"):
        page =page+1;
        url =url.replace("page="+str(page-1),"page="+str(page))
        print(url)
        driver.get(url)
        Time.sleep(1)
    else:
        break
list.reverse()
for i in list:
    file.write(i+"\n")

file.flush()
file.close()